using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using static WaterCylinder.File;
/// <summary>
/// 加载配置文件   
/// </summary>
public class Config : MonoBehaviour
{
    public static readonly string CONFIG_PATH = dataPath + "/Config/";
    public static readonly string DEFAULT_SETTING = CONFIG_PATH + "setting.properties";
    
    public static Dictionary<string, object> deafultSetting = new Dictionary<string, object>();

    /// <summary>
    /// 获取配置
    /// </summary>
    /// <returns></returns>
    public static T GetSetting<T>(string key, string section = "Setting"){
        string query = section + "." + key;
        if(!deafultSetting.ContainsKey(query)){
            INIParser ini = new INIParser();
            ini.Open(DEFAULT_SETTING);
            object res = default;
            Type t = typeof(T);
            if(t == typeof(string)){
                res = ini.ReadValue(section, key, "");
            }else if(t == typeof(int)){
                res = ini.ReadValue(section, key, 0);
            }else if(t == typeof(bool)){
                res = ini.ReadValue(section, key, false);
            }else if(t == typeof(float)){
                res = ini.ReadValue(section, key, 0f);
            }else if(t == typeof(double)){
               res = ini.ReadValue(section, key, 0.0);
            }
            deafultSetting.Add(query, res);
            ini.Close();
        }
        return (T)deafultSetting[query];
    }
    /// <summary>
    /// 改变配置
    /// </summary>
    /// <param name="key"></param>
    /// <param name="value"></param>
    /// <returns></returns>
    public static string SetSetting(string key, string value, string section = "Setting"){
        string query = section + "." + key;
        value = value.Trim();
        if(!deafultSetting.ContainsKey(query)){
            deafultSetting.Add(query, value);
        }
        INIParser ini = new INIParser();
        ini.Open(DEFAULT_SETTING);
        ini.WriteValue(section, key, value);
        ini.Close();
        return deafultSetting[query] as string;
    }
    /// <summary>
    /// 获取配置文件INI
    /// </summary>
    /// <param name="path"></param>
    /// <returns></returns>
    public static INIParser GetConfig(string path){
        INIParser ini = new INIParser();
        ini.Open(CONFIG_PATH + path);
        return ini;
    }

    /// <summary>
    /// 加载配置文件
    /// </summary>
    public static void LoadConfig(){
       GameManager.instance.playerConfig = new PlayerConfig();
       GameManager.instance.playerConfig.Load();
    }
}
